
package me.zhengjie.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.base.AjaxResult;
import me.zhengjie.domain.AppHoliday;
import me.zhengjie.service.AppHolidayService;
import me.zhengjie.service.dto.AppHolidayQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author IQW
* @date 2022-07-18
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "请假管理管理")
@RequestMapping("/api/holiday")
public class ApiHolidayController {

    private final AppHolidayService appHolidayService;

    @GetMapping("/cate")
    @ApiOperation("请假类别")
    public ResponseEntity<Object> queryAppHolidayCate(){
        return new ResponseEntity<>(appHolidayService.queryAppHolidayCate(),HttpStatus.OK);
    }

    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<Object> queryAppHoliday(@RequestParam int type,
                                                  AppHolidayQueryCriteria criteria,
                                                  @PageableDefault(sort = {"holId"}, direction = Sort.Direction.DESC) Pageable pageable){
        //类型 1我的请假，2请假审批（3已审批）
        Integer userId = SecurityUtils.getCurrentUserId().intValue();
        if(type ==1){
            criteria.setUserId(userId);
        }else{
            //状态 0待审批、1通过、2不通过、3撤销
            if(type == 3){
                //查看历史
                criteria.setStatusIn(new HashSet(Arrays.asList(1,2)));

            }else{
                //查看当前待审批
                criteria.setStatus(0);
            }
            criteria.setUserId1(userId);
        }
        return new ResponseEntity<>(appHolidayService.queryAllApi(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/detail")
    @ApiOperation("详情")
    public Map detail(@RequestParam Integer holId){
        return AjaxResult.success(appHolidayService.findById(holId));
    }

    @PostMapping("/add")
    @ApiOperation("新增")
    public Map createAppHoliday(@Validated @RequestBody AppHoliday resources){
        return appHolidayService.add(resources);
    }

    @PostMapping("/act")
    @ApiOperation("审批")
    public Map actAppHoliday(@Validated @RequestBody AppHoliday resources){
        return appHolidayService.act(resources);
    }

    @PostMapping("/cancel")
    @ApiOperation("取消")
    public Map cancelAppHoliday(@Validated @RequestBody AppHoliday resources){
        return appHolidayService.cancel(resources);
    }


}